<!doctype html>
<html>
<?php
	require_once('globals.php');
	require_once('inc/weixiconfig.php');
?>
<head>
<meta charset="UTF-8">
<meta content="initial-scale=0.5,user-scalable=no,width=device-width" name="viewport" />
<meta content="telephone=no" name="format-detection" />
<title>屋童树公开课报名</title>
<link href="static/css/wx.css" rel="stylesheet" type="text/css" />
<link href="https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet">
</head>

<body>
	<form>
		<div class="contactinfo orderpage">
			<p class="tip-alert-info" style="margin-top:0">
				报名成功后，我们工作人员将在24小时内与您联系。<br />
				我们的联系电话号码：<b>0571-28284036</b>
			</p>
			<ul>
				<li class="kidname"><span>孩子姓名：<input class="name" type="text" name="kidname" value="" /></span></li>
				<li>
					<span>孩子性别：
						<select id="issex" name="issex">
							<option value="0">请选择</option>
							<option value="1">男</option>
							<option value="2">女</option>
						</select>
					</span>
				</li>
				<li class="kidage"><span>孩子年龄：
						<select id="kidage" name="kidage">
							<option value="0">请正确选择</option>
							<option value="7">7周岁</option>
							<option value="8">8周岁</option>
							<option value="9">9周岁</option>
							<option value="10">10周岁</option>
							<option value="11">11周岁</option>
							<option value="12">12周岁</option>
							<option value="13">13周岁</option>
						</select>
            		</span></li>
				<li class="parenttel"><span>家长电话：<input class="per_phone" type="text" name="parenttel" value="" maxlength="11" /></span></li>
				<li class="parentname"><span>家长姓名：<input class="per_name" type="text" name="parentname" value="" /></span></li>
			</ul>
		</div>

		<div class="order-introduce" id="introl"></div>
		<script type="text/html" id="introlTemp">
			<h2>课程介绍</h2>
			<p>
				<span><b>报名课程：</b><%=data.name%></span><br>
				<span><b>课程费用：</b><%=data.price%>元</span><br>
				<span><b>预付费用：</b><%=data.subsist%>元</span><br>
				<span><b>上课时间：</b><%=data.class_time%></span><br>
				<span><b>上课地址：</b><%=data.address%></span><br>
				<span><b>课程内容简介：</b></span><br>
				<span><%=data.introduction%></span>
			</p>
			<div class="bottombar"><span>课程费用：<b>¥ <%=data.subsist%></b></span><input type="button" id="submitbt" value="报名缴费" class="submitbotton" data-id='<%=data.tbid%>' /></div>
		</script>
	</form>
</body>
<script src="http://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script src="http://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.js"></script>
<script src="js/aui-art-template-a880d14/lib/template-web.js" charset="utf-8"></script>
<script type="text/javascript">

	$.ajax({
		type : "GET",
		async:false,
		url: 'http://www.familyandtree.com/getcontent.php?ac=class_info',
		data:{
			type:1
		},
		dataType : "json",
		success : function(data){
			if (data.error != '') {
				swal({
					button: false,
					title: data.error,
				});
				return false;
			}else {
				template.defaults.escape=false;
				$('#introl').append(template('introlTemp',data));
			}
		},
		error:function(data){
			swal({
				button: false,
				title: data.error,
			});
		}
	});

	$('#submitbt').click(function(){
		if ($('.name').val() == '' || $('#kidage').val() == '' || $('.per_phone').val() == '' || $('.per_name').val() == '') {
			swal({
				button: false,
				title: '请完善个人信息',
			});
			return false;
		}
		if(!(/^1(3|4|5|7|8)\d{9}$/.test($('.per_phone').val()))){
			swal({
				button: false,
				title: '请输入正确的手机号~',
			});
			return false;
		}
		$.ajax({
			type : "POST",
			async:false,
			url: 'http://www.familyandtree.com/getcontent.php?ac=gen_order',
			data:{
				type:2,
				class_id:$(this).data('id'),
				ch_name:$('.name').val(),
				ch_sex:$('#issex').val(),
				ch_age:$('#kidage').val(),
				p_phone:$('.per_phone').val(),
				p_name:$('.per_name').val()
			},
			dataType : "json",
			success : function(data){
				if (data.error != '') {
					swal({
						button: false,
						title: data.error,
					});
					return false;
				}else {
					getpaydetail(data.order_no);
				}
			},
			error:function(data){
				swal({
					button: false,
					title: data.error,
				});
			}
		});
	})

	function getpaydetail(ordernumber){
		$.ajax({
			type : "POST",
			async:false,
			url: 'http://www.familyandtree.com/getcontent.php?ac=trade_pay',
			data:{
				ordernumber:ordernumber
			},
			dataType : "json",
			success : function(data){
				console.log(data);
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						"appId" : "wx58c39a575f4eec96",     //公众号名称，由商户传入
						"timeStamp": data.data.timestamp,         //时间戳，自1970年以来的秒数
						"nonceStr" : data.data.noncestr, //随机串
						"package" : "prepay_id="+data.data.prepay_id,
						"signType" : "MD5",         //微信签名。方式：
						"paySign" : data.data.sign //微信签名
					},
					function(res){
						if(res.err_msg == "get_brand_wcpay_request:ok" ) {
							window.location.href='order_list.php'
						}else if(res.err_msg == "get_brand_wcpay_request:cancel" ) {
							swal({
								button: false,
								title: '交易已取消~',
							});
						}else{
							swal({
								button: false,
								title: '支付失败！',
							});
						}     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
					}
				);
			},
			error:function(data){
				swal({
					button: false,
					title: data.error,
				});
			}
		});
	}
	function isPhoneNo(phone) {
		var pattern = /^1[34578]\d{9}$/;
		return pattern.test(phone);
	}
</script>
</html>
